layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage,
        $ = layui.jquery;

    //添加权限
    form.on("submit(addTree)", function (data) {
        var addTree = '{';
        var count = 0;
        var obj = document.all.authority;
        var flag = 0;
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                if (flag == 0) {
                    addTree += '"' + count + '":"' + parseInt(obj[i].id.substring(10)) + '"';
                    flag++;
                    count++;
                } else {
                    addTree += ',"' + count + '":"' + parseInt(obj[i].id.substring(10)) + '"';
                    count++;
                }
            }
        }
        addTree += '}';
        $.ajax({
            url: "http://localhost/ci/admin/tree/imp_add",
            type: "post",
            data: {'role_id': parent.$('.ci_data').val(), 'data': addTree, 'token': localStorage.token},
            dataType: "json",
            success: function (data) {
                console.log(data);
                top.layer.msg("权限修改成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            },
            error: function (data) {
                console.log(data);
                top.layer.msg("权限修改失败！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }
        })
        return false;
    })
})